//
//  PhoneAreaTableController.m
//  objective_c_language
//
//  Created by 王玉龙 on 2018/6/1.
//  Copyright © 2018年 longlong. All rights reserved.
//

#define CellIdentifer @"PhoneAreaTableCell"
#define HeaderIdentifer @"PhoneAreaSectionHeaderView"


#import "PhoneAreaTableController.h"
#import "PhoneAreaResultTableViewController.h"
#import <Masonry.h>
#import "PhoneAreaTableCell.h"
#import "PhoneAreaSectionHeaderView.h"
#import "PhoneAreaCodeModel.h"

@interface PhoneAreaTableController ()
<UISearchBarDelegate,UISearchControllerDelegate,
UISearchBarDelegate>

@property (nonatomic ,strong) NSMutableArray<NSArray<PhoneAreaCodeModel *> *> *dataArray;
@property (nonatomic, strong) PhoneAreaResultTableViewController *searchResultController;
@property (nonatomic, strong) UISearchController *searchController;


@end

@implementation PhoneAreaTableController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    [self.tableView registerClass:PhoneAreaTableCell.class forCellReuseIdentifier:CellIdentifer];
    [self.tableView registerClass:[PhoneAreaSectionHeaderView class] forHeaderFooterViewReuseIdentifier:HeaderIdentifer];
    self.tableView.tableHeaderView = self.searchController.searchBar;
    [self loadNetworkData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray[section].count;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    cell.layer.transform = CATransform3DMakeScale(0.1, 0.1, 1);
    //x和y的最终值为1
    [UIView animateWithDuration:0.5 animations:^{
        cell.layer.transform = CATransform3DMakeScale(1, 1, 1);
    }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PhoneAreaTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifer forIndexPath:indexPath];
    cell.codeModel = self.dataArray[indexPath.section][indexPath.row];
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    PhoneAreaSectionHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HeaderIdentifer];
    headerView.codeModel = self.dataArray[section].firstObject;
    return headerView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark -  UISearchBarDelegate

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    [self setSearchBarIconIsCenter:NO];
    return YES;
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    UIButton *button = [_searchController.searchBar valueForKey:@"_cancelButton"];
    if (button)
    {
        [button setTitleColor:COLOR_VALUE(0x777777, 1) forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Medium" size:17];
    }
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [self setSearchBarIconIsCenter:YES];
}

#pragma mark - private method

- (void)setSearchBarIconIsCenter:(BOOL)isCenter
{
    if (@available(iOS 11.0, *))
    {
        if (isCenter)
        {
            [_searchController.searchBar setPositionAdjustment:UIOffsetMake((ScreenWidth - 140)/2.0, 0) forSearchBarIcon:UISearchBarIconSearch];
        }
        else
        {
            [_searchController.searchBar setPositionAdjustment:UIOffsetZero forSearchBarIcon:UISearchBarIconSearch];
        }
    }
}

#pragma mark - Network Request

- (void)loadNetworkData
{
    __weak typeof(self) weakSelf = self;
    [PhoneAreaCodeModel loadPhoneCodeDataOriginalData:^(NSMutableArray<PhoneAreaCodeModel *> *modelArray)
    {
        weakSelf.searchResultController.modelArray = modelArray;
    } SectionData:^(NSMutableArray<NSArray<PhoneAreaCodeModel *> *> *sectionArray)
    {
        weakSelf.dataArray = sectionArray;
        [weakSelf.tableView reloadData];
    }];
}

#pragma mark - setter and getters

- (NSMutableArray<NSArray<PhoneAreaCodeModel *> *> *)dataArray
{
    if (_dataArray == nil)
    {
        _dataArray = [NSMutableArray array];
    }
    
    return  _dataArray;
}

- (UISearchController *)searchController
{
    if (_searchController == nil)
    {
//        resultVC.delegate = self;
        _searchController = [[UISearchController alloc] initWithSearchResultsController:self.searchResultController];
        [_searchController.searchBar sizeToFit];
        _searchController.searchResultsUpdater = self;
        self.definesPresentationContext = YES;
        _searchController.searchBar.placeholder = @"搜索国家/地区";
        _searchController.searchResultsUpdater = self.searchResultController;
        
        UIView *view = _searchController.searchBar.subviews[0];
        for (UIView *subView in view.subviews)
        {
            if ([subView isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
            {
                subView.backgroundColor = [UIColor whiteColor];
                [subView removeFromSuperview];
            }
            else if ([subView isKindOfClass:NSClassFromString(@"UISearchBarTextField")])
            {
                subView.backgroundColor = COLOR_RGBA(142, 142, 142, 0.12);
            }
        }
        
        [self setSearchBarIconIsCenter:YES];
        
        _searchController.searchBar.delegate = self;
        
    }
    return _searchController;
}

- (PhoneAreaResultTableViewController *)searchResultController
{
    if (_searchResultController == nil)
    {
        _searchResultController = [[PhoneAreaResultTableViewController alloc] init];
    }
    return _searchResultController;
}



@end
